"""
@Time : 2021/9/16 13:04 
@Author : 常雷
@File : t_23字符串压缩.py 
@Software: PyCharm
"""
# 把字符串aaabbcccd这种形式的字符串压缩成a3b2c3d1这种形式
'''
先记住开头字母并且后面跟1
往后看后面是不是开头字母
    是:a后面数加一并且继续遍历字符串
    否:当做新开头
'''
s = 'aaabbcccd'
# f = s[0]
# count = 1
# result = ""
# for i in range(1, len(s)):
#     if s[i] == f:
#         count += 1
#     else:
#         # f和count的记录和更新
#         result += f + str(count)
#         f = s[i]
#         count = 1
#
# result += f + str(count)
#
# print(result)


# 方法二:递归
def ys(sr, result=""):
    if sr == "":
        return result
    f = sr[0]
    count = 1
    if len(sr) == 1:
        return f + str(count)
    for i in list(range(1, len(sr))):
        if sr[i] == f:
            count += 1
        else:
            return result + f + str(count) + ys(sr[count:])

print(ys(s))

